package fristphase.day9_06.mysubmarinegame;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class SeaObject {
    private static final int LIVE = 0;
    private static final int DEAD = 1;
    public int state = LIVE;
    protected int width,height,x,y,speed;

    SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random random = new Random();
        y = random.nextInt(479-150-height)+150;
        speed = random.nextInt(3)+1;
    }
    SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public void goDead(){
        state = DEAD;
    }
    public abstract void move();
    public abstract ImageIcon getImage();
    //越界判断
    public boolean isDeleteSubmarineJudge(){
        return x>=World.WIDTH;
    }

    //碰撞判断
    public boolean isHit(SeaObject other){
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        return x1 <= other.x && x2>= other.x && y1 <= other.y && y2 >= other.y;
    }


    public boolean isDead(){
        return state == DEAD;
    }
    public boolean isLive(){
        return state == LIVE;
    }
    public void paintImage(Graphics g){
        if(state == LIVE){
            this.getImage().paintIcon(null,g,x,y);
        }
    }
}
